/* 
 * Copyright 2011 Carnegie Mellon University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.cmu.lti.dimple;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import edu.cmu.lti.dimple.PairFilter;
import edu.cmu.lti.dimple.PairMatchingEvaluation;
import edu.cmu.lti.dimple.Parameters;
import edu.cmu.lti.dimple.data.MSParaphrasePair;
import edu.cmu.lti.dimple.data.MSParaphrasePairLoader;
import edu.cmu.lti.dimple.data.ComplexQAEvalPair;
import edu.cmu.lti.dimple.data.ComplexQAEvalPairLoader;
import edu.cmu.lti.dimple.data.RTEPair;
import edu.cmu.lti.dimple.data.RTEPairLoader;
import edu.cmu.lti.dimple.mavuno.LearnedParaphrases;

/**
 * Executes a set of extrinsic tasks.
 * 
 * @author Hideki Shima
 *
 */
public class ExtrinsicTaskLauncher {

  private Set<String> sizeLog = new LinkedHashSet<String>();
  
  public List<Double> calcExtrinsicRTE( List<RTEPair> rtePairs, 
          List<LearnedParaphrases> lps, 
          String targetTerm, int listSize ) {

    PairFilter<RTEPair> pf = new PairFilter<RTEPair>();
    
    if (rtePairs==null) {
      File dir = new File(Parameters.resourceDir, "rte");
      List<RTEPair> pairs1 = RTEPairLoader.load(new File(dir, "RTE1_dev.xml"));
      List<RTEPair> pairs2 = RTEPairLoader.load(new File(dir, "RTE1_dev2.xml"));
      List<RTEPair> pairs3 = RTEPairLoader.load(new File(dir, "RTE1_test.xml"));
      List<RTEPair> pairs4 = RTEPairLoader.load(new File(dir, "RTE2_dev.xml"));
      List<RTEPair> pairs5 = RTEPairLoader.load(new File(dir, "RTE2_test.xml"));
      List<RTEPair> pairs6 = RTEPairLoader.load(new File(dir, "RTE3_pairs_dev-set-final.xml"));
      List<RTEPair> pairs7 = RTEPairLoader.load(new File(dir, "RTE3-TEST-GOLD.xml"));
      List<RTEPair> pairs8 = RTEPairLoader.load(new File(dir, "RTE4_TEST-SET_GOLD.xml"));
  
      List<RTEPair> pairs = new ArrayList<RTEPair>();
      pairs.addAll(pairs1);
      pairs.addAll(pairs2);
      pairs.addAll(pairs3);
      pairs.addAll(pairs4);
      pairs.addAll(pairs5);
      pairs.addAll(pairs6);
      pairs.addAll(pairs7);
      pairs.addAll(pairs8);
      sizeLog.add("RTE\tALL\t"+pairs.size()+"\t");
      
      List<RTEPair> pairsA = pf.filterByLabel(pairs, "TRUE");
      List<RTEPair> pairsB = pf.filterByLabel(pairs, "YES");
      List<RTEPair> pairsC = pf.filterByLabel(pairs, "ENTAILMENT");
      pairs = new ArrayList<RTEPair>();
      pairs.addAll(pairsA);
      pairs.addAll(pairsB);
      pairs.addAll(pairsC);
      sizeLog.add("RTE\tPOS\t"+pairs.size()+"\t");
      
      rtePairs = pairs;
    }
    
    List<RTEPair> pairs = Parameters.xorMode 
      ? pf.filterByXOR(rtePairs, targetTerm) 
      : pf.filterByWord(rtePairs, targetTerm);
    pf.removeDuplicated(pairs);

    sizeLog.add("RTE\t"+targetTerm+"\t"+pairs.size()+"\tfiltered");
   
    PairMatchingEvaluation<RTEPair> pe = new PairMatchingEvaluation<RTEPair>();
    return pe.evaluate(lps, targetTerm, pairs, "RTE_"+targetTerm, listSize);
  }
  
  public List<Double> calcExtrinsicMSRPC( List<MSParaphrasePair> msrpcPairs, 
          List<LearnedParaphrases> lps, 
          String targetTerm, int listSize ) {
    PairFilter<MSParaphrasePair> pf = new PairFilter<MSParaphrasePair>();
    if ( msrpcPairs==null ) {
      File dir = new File(Parameters.resourceDir, "msrpc");
      List<MSParaphrasePair> pairs1 = MSParaphrasePairLoader.load(
              new File(dir,"msr_paraphrase_train.txt"));
      List<MSParaphrasePair> pairs2 = MSParaphrasePairLoader.load(
              new File(dir,"msr_paraphrase_test.txt"));
      List<MSParaphrasePair> pairs = new ArrayList<MSParaphrasePair>();
      pairs.addAll(pairs1);
      pairs.addAll(pairs2);
      
      sizeLog.add("MSRPC\tALL\t"+pairs.size());
      pairs = pf.filterByLabel(pairs, "1");
      sizeLog.add("MSRPC\tPOS\t"+pairs.size());
      msrpcPairs = pairs;
    }
   
    List<MSParaphrasePair> pairs = Parameters.xorMode 
      ? pf.filterByXOR(msrpcPairs, targetTerm) 
      : pf.filterByWord(msrpcPairs, targetTerm);
    pf.removeDuplicated(pairs);
    sizeLog.add("MSRPC\t"+targetTerm+"\t"+pairs.size()+"\tfiltered");
    
    PairMatchingEvaluation<MSParaphrasePair> pe = new PairMatchingEvaluation<MSParaphrasePair>();
    return pe.evaluate(lps, targetTerm, pairs, "MSRPC_"+targetTerm, listSize);
  }
  
  public List<Double> calcExtrinsicQA( List<ComplexQAEvalPair> qaPairs,
          List<LearnedParaphrases> lps, String targetTerm, int listSize ) {
    if ( qaPairs==null ) {
      File dir = new File(Parameters.resourceDir,"qa");
      List<ComplexQAEvalPair> pairs1 = ComplexQAEvalPairLoader.load(new File(dir, "2007_other/2007_other_judgments"), 
              new File(dir, "2007_other/other_nugget_pyramid.txt"));
      List<ComplexQAEvalPair> pairs2 = ComplexQAEvalPairLoader.load(new File(dir, "2007_ciqa/2007_ciqa_judgments"), 
              new File(dir, "2007_ciqa/ciqa_nugget_pyramid.txt"));
      List<ComplexQAEvalPair> pairs3 = ComplexQAEvalPairLoader.load(new File(dir, "2006_other/other_judgments"), 
              new File(dir, "2006_other/06.other_answers.txt"));
      List<ComplexQAEvalPair> pairs4 = ComplexQAEvalPairLoader.load(new File(dir, "2006_ciqa/ciqa_judgments"), 
              new File(dir, "2006_ciqa/06.ciqa_answers.txt"));
      List<ComplexQAEvalPair> pairs5 = ComplexQAEvalPairLoader.load(new File(dir, "2005_other/other"), 
              new File(dir, "2005_other/05.nuggets.other.answers.txt"));
      List<ComplexQAEvalPair> pairs6 = ComplexQAEvalPairLoader.load(new File(dir, "2005_relation/relate.results"), 
              new File(dir, "2005_relation/05.nuggets.list.txt"));
      List<ComplexQAEvalPair> pairs7 = ComplexQAEvalPairLoader.load(new File(dir, "2005_relation/relate.results"), 
              new File(dir, "2005_relation/05.nuggets.list.txt"));
      List<ComplexQAEvalPair> pairs = new ArrayList<ComplexQAEvalPair>();
      pairs.addAll(pairs1);
      pairs.addAll(pairs2);
      pairs.addAll(pairs3);
      pairs.addAll(pairs4);
      pairs.addAll(pairs5);
      pairs.addAll(pairs6);
      pairs.addAll(pairs7);
      sizeLog.add("QA\tALL\t"+pairs.size()+"\t");
      qaPairs = pairs;
    }

    PairFilter<ComplexQAEvalPair> pf = new PairFilter<ComplexQAEvalPair>();
    List<ComplexQAEvalPair> pairs = Parameters.xorMode 
      ? pf.filterByXOR(qaPairs, targetTerm) 
      : pf.filterByWord(qaPairs, targetTerm);
//    pairs = pf.filterByLabel(pairs, "1");
    pf.removeDuplicated(pairs);

    sizeLog.add("QA\t"+targetTerm+"\t"+pairs.size()+"\tfiltered");
    
    PairMatchingEvaluation<ComplexQAEvalPair> pe = new PairMatchingEvaluation<ComplexQAEvalPair>();
    return pe.evaluate(lps, targetTerm, pairs, "QA_"+targetTerm, listSize);
  }

  public Set<String> getSizeLog() {
    return sizeLog;
  }
  
  
}
